package com.xwn.utils.core;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.xwn.utils.core.T.SpringUtils;

/**
 * @Author duandw
 * @Date 2019/12/31 17:46
 **/
public class DubboUtilsImpl {
    public DubboUtilsImpl() {
    }

    public static <T> T getReference(Class<T> clazz, String version) {
        ReferenceConfig<T> referenceConfig = new ReferenceConfig();
        ApplicationConfig application = (ApplicationConfig) com.xwn.utils.core.T.SpringUtils.getBean(ApplicationConfig.class);
        referenceConfig.setApplication(application);
        RegistryConfig regist = (RegistryConfig) com.xwn.utils.core.T.SpringUtils.getBean(RegistryConfig.class);
        referenceConfig.setRegistry(regist);
        referenceConfig.setInterface(clazz);
        if (!StringUtils.isEmpty(version)) {
            referenceConfig.setVersion(version);
        }

        return referenceConfig.get();
    }

    public static <T> T getReference(Class<T> clazz, String group, String version) {
        ReferenceConfig<T> referenceConfig = new ReferenceConfig();
        ApplicationConfig application = (ApplicationConfig)SpringUtils.getBean(ApplicationConfig.class);
        referenceConfig.setApplication(application);
        RegistryConfig regist = (RegistryConfig)SpringUtils.getBean(RegistryConfig.class);
        referenceConfig.setRegistry(regist);
        referenceConfig.setInterface(clazz);
        referenceConfig.setGroup(group);
        if (!StringUtils.isEmpty(version)) {
            referenceConfig.setVersion(version);
        }

        return referenceConfig.get();
    }

    public static <T> T getReference(Class<T> clazz) {
        return getReference(clazz, (String)null);
    }
}
